<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['modeldataset:modeldataset:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['modeldataset:modeldataset:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['modeldataset:modeldataset:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['modeldataset:modeldataset:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modeldatasetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="模型名称" align="center" prop="modelName" />
      <el-table-column label="模型类型" align="center" prop="modelType" />
      <el-table-column label="模型版本" align="center" prop="modelVersion" />
      <el-table-column label="模型描述" align="center" prop="modelDescription" />
<!--      <el-table-column label="文件路径" align="center" prop="modelPath" />-->

      <el-table-column label="模型路径" align="center" prop="modelPath" width="180">
        <!-- 自定义单元格模板：通过 slot-scope="scope" 获取当前行数据 -->
        <template slot-scope="scope">
          <!-- 1. 绑定路径：scope.row.模型路径字段名（此处用 modelPath 示例） -->
          <!-- 2. 处理空值：如果路径为空，显示“无”而非空链接 -->
          <a
            :href="scope.row.modelPath"
            target="_blank"
            v-if="scope.row.modelPath"
            style="color: #409EFF; text-decoration: underline;"
          >
            下载模型
          </a>
          <span v-else>等待上传</span> <!-- 路径为空时显示的内容 -->
        </template>
      </el-table-column>
      <el-table-column label="文件大小(字节)" align="center" prop="modelSize" />
      <el-table-column label="模型格式" align="center" prop="modelFormat" />
      <el-table-column label="训练框架" align="center" prop="framework" />
      <el-table-column label="模型准确率" align="center" prop="accuracy" />
      <el-table-column label="模型精确率" align="center" prop="modelPrecision" />
      <el-table-column label="模型召回率" align="center" prop="recall" />
      <el-table-column label="F1分数" align="center" prop="f1Score" />
      <el-table-column label="训练数据集" align="center" prop="trainingDataset" />
      <el-table-column label="输入形状" align="center" prop="inputShape" />
      <el-table-column label="输出形状" align="center" prop="outputShape" />
      <el-table-column label="分类数量" align="center" prop="classesCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['modeldataset:modeldataset:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['modeldataset:modeldataset:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型库数据集对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="模型版本" prop="modelVersion">
          <el-input v-model="form.modelVersion" placeholder="请输入模型版本" />
        </el-form-item>
        <el-form-item label="模型描述" prop="modelDescription">
          <el-input v-model="form.modelDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文件路径" prop="modelPath">
          <file-upload v-model="form.modelPath"/>
        </el-form-item>
        <el-form-item label="文件大小(字节)" prop="modelSize">
          <el-input v-model="form.modelSize" placeholder="请输入文件大小(字节)" />
        </el-form-item>
        <el-form-item label="模型格式" prop="modelFormat">
          <el-input v-model="form.modelFormat" placeholder="请输入模型格式" />
        </el-form-item>
        <el-form-item label="训练框架" prop="framework">
          <el-input v-model="form.framework" placeholder="请输入训练框架" />
        </el-form-item>
        <el-form-item label="模型准确率" prop="accuracy">
          <el-input v-model="form.accuracy" placeholder="请输入模型准确率" />
        </el-form-item>
        <el-form-item label="模型精确率" prop="modelPrecision">
          <el-input v-model="form.modelPrecision" placeholder="请输入模型精确率" />
        </el-form-item>
        <el-form-item label="模型召回率" prop="recall">
          <el-input v-model="form.recall" placeholder="请输入模型召回率" />
        </el-form-item>
        <el-form-item label="F1分数" prop="f1Score">
          <el-input v-model="form.f1Score" placeholder="请输入F1分数" />
        </el-form-item>
        <el-form-item label="训练数据集" prop="trainingDataset">
          <file-upload v-model="form.trainingDataset"/>
        </el-form-item>
        <el-form-item label="输入形状" prop="inputShape">
          <el-input v-model="form.inputShape" placeholder="请输入输入形状" />
        </el-form-item>
        <el-form-item label="输出形状" prop="outputShape">
          <el-input v-model="form.outputShape" placeholder="请输入输出形状" />
        </el-form-item>
        <el-form-item label="分类数量" prop="classesCount">
          <el-input v-model="form.classesCount" placeholder="请输入分类数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModeldataset, getModeldataset, delModeldataset, addModeldataset, updateModeldataset } from "@/api/modeldataset/modeldataset";

export default {
  name: "Modeldataset",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型库数据集表格数据
      modeldatasetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        modelName: [
          { required: true, message: "模型名称不能为空", trigger: "blur" }
        ],
        modelVersion: [
          { required: true, message: "模型版本不能为空", trigger: "blur" }
        ],
        modelDescription: [
          { required: true, message: "模型描述不能为空", trigger: "blur" }
        ],
        modelPath: [
          { required: true, message: "文件路径不能为空", trigger: "blur" }
        ],
        modelSize: [
          { required: true, message: "文件大小(字节)不能为空", trigger: "blur" }
        ],
        modelFormat: [
          { required: true, message: "模型格式不能为空", trigger: "blur" }
        ],
        trainingDataset: [
          { required: true, message: "训练数据集不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模型库数据集列表 */
    getList() {
      this.loading = true;
      listModeldataset(this.queryParams).then(response => {
        this.modeldatasetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelName: null,
        modelType: null,
        modelVersion: null,
        modelDescription: null,
        modelPath: null,
        modelSize: null,
        modelFormat: null,
        framework: null,
        accuracy: null,
        modelPrecision: null,
        recall: null,
        f1Score: null,
        trainingDataset: null,
        inputShape: null,
        outputShape: null,
        classesCount: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型库数据集";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getModeldataset(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模型库数据集";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateModeldataset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModeldataset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模型库数据集编号为"' + ids + '"的数据项？').then(function() {
        return delModeldataset(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('modeldataset/modeldataset/export', {
        ...this.queryParams
      }, `modeldataset_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
